package engine.expression;

public enum OperatorsUnary implements Operators
{
	Moins("-",  (x,y) -> -x),
	Factorial("!", (x,y) -> factorial(new Double(x).intValue()));
	
	private String representation;
	private Calculable calcul;
	
	private OperatorsUnary(String representation, Calculable calcul)
	{
		this.representation = representation;
		this.calcul = calcul;
	}
	
	public String toString()
	{
		return representation;
	}
	
	public double calculate(double x, double y)
	{
		if(y != 0) throw new IllegalArgumentException("Le second paramètre d'un opérateur unaire doit être 0.");
		
		return calcul.calculate(x,y);
	}
	
	private static int factorial(int x)
	{
		if(x == 0) return 1;
		else return factorial(x-1)*x;
	}
	
	public boolean unary()
	{
		return true;
	}
}
